package api

import (
	"errors"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/mysql"
	"net"

	"github.com/labstack/echo/v4"
	"longmen/server/pkg/common/api_models"
)

// @Summary Ip归属地查询 不需登陆
// @Description Ip归属地查询 不需登陆
// @Accept  json
// @Produce  json
// @Tags IP
// @Param   sendBody body api_models.GetIpSearchReqReq true "body"
// @Success 200  "result"
// @Router /api/ip/search [POST]
func (base *Controller) IpToArea(c echo.Context) error {
	//fName := "IpToArea"
	r := &api_models.GetIpSearchReqReq{}
	if err := util.Json2Struct(c.Get("body").(string), r); err != nil {
		return util.BuildFail(c, 1, "", errors.New("解析JSON串IP地址出错"))
	}
	parseIP := net.ParseIP(r.Ip)
	if parseIP == nil {
		return util.BuildFail(c, 1, "", errors.New("不正确的ip地址"))
	}
	resp, err := mysql.GetIpToArea(parseIP.String())
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, resp, "")
}

// @Summary Ip 视频区域限制 不需登陆
// @Description 视频区域限制 不需登陆
// @Accept  json
// @Produce  json
// @Tags IP
// @Param   sendBody body api_models.GetIpAstrictAreaReq true "body"
// @Success 200  "result"
// @Router /api/ip/astrict_area [POST]
func (base *Controller) AstrictArea(c echo.Context) error {

	r := &api_models.GetIpAstrictAreaReq{}
	if err := util.Json2Struct(c.Get("body").(string), r); err != nil {
		return util.BuildFail(c, 1, "", errors.New("解析JSON串IP地址出错"))
	}
	parseIP := net.ParseIP(r.Ip)
	if parseIP == nil {
		return util.BuildFail(c, 1, "", errors.New("不正确的ip地址"))
	}
	resp, err := mysql.MatchAstrictArea(r)
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, resp, "")
}
